<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="autoWidth">
        <div class="toggle-button" @click="toggleCollapse">| | |</div>
        <el-menu
          background-color="#333333"
          text-color="#fff"
          active-text-color="#359bff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 创建一个空数组用来保存后台返回的数据
      menuList: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-lock',
        101: 'el-icon-s-goods',
        102: 'el-icon-tickets',
        145: 'el-icon-data-line',
      },
      isCollapse: false,
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  name: 'Home',
  computed: {
    autoWidth() {
      return this.isCollapse ? '64px' : '200px'
    },
  },
  methods: {
    logout() {
      // 清空用户的token
      window.sessionStorage.clear()
      // 编程路由导航退回登录页面
      this.$router.push('/login')
    },
    // 数据接口的数据操作返回的是promise，用async 在函数名之前使其变成一个异步函数
    async getMenuList() {
      // es6数组的解构赋值，拿到其中的data并赋值为res
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 拿到数据之后就交给data保存
      this.menuList = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>

<style scoped>
.home_container {
  height: 100%;
}
.toggle-button {
  background-color: #475165;
  font-size: 10px;
  line-height: 30px;
  color: #fff;
  letter-spacing: 0.2em;
  text-align: center;
  cursor: pointer;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #363d40;
  color: #fff;
  text-align: center;
  line-height: 60px;
}
.el-aside {
  background-color: #333333;
  color: #333;
  /* text-align: center; */
  line-height: 200px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
}
.el-menu {
  border-right: 0;
}
.el-button {
  cursor: pointer;
  margin: 10px 0;
}
</style>
